package org.spring.demo.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.spring.demo.dao.SysRoleMapper;
import org.spring.demo.dao.SysUserMapper;
import org.spring.demo.entity.City;
import org.spring.demo.entity.SysRole;
import org.spring.demo.entity.SysUser;
import org.spring.demo.sdk.WXPay;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户Service
 * @author zp
 * @date   2018年7月23日
 */
@Service
public class SysUserService {

	@Autowired
    @Qualifier("bdlpdbMongoTemplate")
    private MongoTemplate bdlpdbMongoTemplate;
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysRoleMapper sysRoleMapper;
	/**
	 *下面注入WX Bean用了两种模式
	 *第一种 @Resource(name="appPayCon") 这个可以在项目中的配置类（BeanConfig）中用@Bean注解注入
	 *第二种 @Autowired 引入的。这种是用的xml文件中配置的Bean
	 **/
	//@Resource(name="appPayCon")
	@Autowired
	private WXPay wxPay;
	@Transactional
	public SysUser add(String flag) {
		SysUser users = sysUserMapper.selectByPrimaryKey(1l);
		Map<String,String> data = new HashMap<String,String>();
		    data.put("body","测试" );
	        data.put("out_trade_no", UUID.randomUUID().toString().replace("-",""));
	        //data.put("device_info", "WEB");
	        data.put("fee_type", "CNY");
	        data.put("total_fee", new Double(1 * 100).intValue() + "");
	        data.put("spbill_create_ip", "123.12.12.123");
	        data.put("notify_url", "http://www.17gp.com/wxpay/notify");
	        data.put("trade_type", "NATIVE");
	        data.put("product_id", "12");
		try {
			Map<String,String> res =wxPay.unifiedOrder(data);
			System.out.println(res);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return users;
	}
	@Transactional(rollbackFor = {RuntimeException.class,Exception.class})
	public int addEntity(){
		SysUser user =new SysUser();
		sysUserMapper.insert(user);
		SysRole role = new SysRole();
		role.setRoleId(4l);
		sysRoleMapper.insert(role);
		return 1;
	}
	public SysUser selectByPrimaryKey(long l) {
		return sysUserMapper.selectByPrimaryKey(l);
		
	}
	public List<City> getAllCityList() {
		List<City> list = bdlpdbMongoTemplate.findAll(City.class);
		return list;
	}

}
